package jobresponse;

import java.util.Enumeration;
import java.util.Random;
import jobadvertisement.JobAdvertisementDAO;
import jobresponse.ViewJobResponseScreen;
import jobseeker.JobSeekerAccountDAO;
import jobseeker.JobSeekerAccountDTO;

/**
 * @author Khine Phyu Phyu Than A0006487B
 */
public class ViewJobResponseController {

    private JobResponseDAO jobResponseDAO;
    private JobAdvertisementDAO jobAdsDAO;
    private JobSeekerAccountDAO jobseekerDAO;

    public void viewJobResponses() {
        addTestData();
        ViewJobResponseScreen jobResponseScreen = new ViewJobResponseScreen();
        jobResponseScreen.setVisible(true);
    }
    
    public void addTestData() {
        jobResponseDAO = new JobResponseDAO();
        jobResponseDAO.readDTOs();
        jobAdsDAO = new JobAdvertisementDAO();
        jobAdsDAO.readDTOs();
        jobseekerDAO = new JobSeekerAccountDAO();
        jobseekerDAO.readDTOs();

        // Create mock data
        if (!jobseekerDAO.readDTOs()) {

            // Create mock jobseeker only if no data is available
            for (int i = 0; i < 5; i++) {
                JobSeekerAccountDTO newJobSeeker = new JobSeekerAccountDTO("jobseeker" + i);
                newJobSeeker.setName("JobSeeker" + i);
                newJobSeeker.setContactNumber("9872321" + i);
                newJobSeeker.setEmail("jobseeker_" + i + "@gmail.com");
                newJobSeeker.setDob("12 Nov 1987");
                jobseekerDAO.addDTO(newJobSeeker);
            }
            jobseekerDAO.writeDTOs();
        }
        System.out.println("Finished creating jobseeker");

        if (jobResponseDAO.getTotal() == 0) {
            // Create mock data only if no jobresponseDAO is available
            Enumeration<String> allJobAdsKeys = jobAdsDAO.getIDs();
            System.out.println("Create new job response");
            while (allJobAdsKeys.hasMoreElements()) {
                Enumeration<String> allJobSeekerKeys = jobseekerDAO.getIDs();
                String currentJobAdsID = allJobAdsKeys.nextElement();
                System.out.println("currentJobAdsID: " + currentJobAdsID);
                while (allJobSeekerKeys.hasMoreElements()) {
                    int id = new Random().nextInt(100000000);
                    JobResponseDTO newJobResponse = new JobResponseDTO("jobresponse" + id);
                    newJobResponse.setJobSeekerID(allJobSeekerKeys.nextElement());
                    newJobResponse.setJobAdvertisementID(currentJobAdsID);

                    if (id % 3 == 0) {
                        newJobResponse.setStatus("Applied");
                    } else if (id % 3 == 1) {
                        newJobResponse.setStatus("Interviewed");
                    } else {
                        newJobResponse.setStatus("Rejected");
                    }
                    jobResponseDAO.addDTO(newJobResponse);
                }
            }
            jobResponseDAO.writeDTOs();
        }
    }
}
